package com.beiing.tianshuai.tianshuai.interest.model;

import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.interest.entity.InterestCategoriesBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Jinni on 2017/11/30 0030
 */

public class InterestPublishModel implements InterestPublishModelImpl {

    private static final String TAG = "InterestPublishModel";
    private OnInterestPublishResponseListener mListener;

    public InterestPublishModel(OnInterestPublishResponseListener listener) {
        mListener = listener;
    }

    @Override
    public void getCategories() {
        Observable<InterestCategoriesBean> observable = DataManager.getInterestCategories();
        Observable<InterestCategoriesBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<InterestCategoriesBean>() {
            @Override
            public void onNext(InterestCategoriesBean bean) {
                if (mListener != null) {
                    mListener.onGetCategoriesSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onGetCategoriesError(e);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void submitVideo(String uid, String tid, String title, String content, String url, File cover) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("uid", uid)
                .addFormDataPart("tid", tid)
                .addFormDataPart("title", title)
                .addFormDataPart("content", content)
                .addFormDataPart("url", url)
                .addFormDataPart("cover", cover.getName(), RequestBody.create(MediaType.parse("image/*"), cover))
                .build();
        Observable<CodeBean> observable = DataManager.getInterestSubmitResult(body);
        Observable<CodeBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(CodeBean bean) {
                if (mListener != null) {
                    mListener.onSubmitSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onSubmitError(e);
                }
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public interface OnInterestPublishResponseListener{
        void onGetCategoriesSuccess(InterestCategoriesBean bean);
        void onGetCategoriesError(Throwable throwable);
        void onSubmitSuccess(CodeBean bean);
        void onSubmitError(Throwable e);
    }
}
